package com.invengo.library_rfid.analyze.utils.uhf.formatHelp;

import com.invengo.library_rfid.analyze.utils.uhf.formatHelp.CBitClass;

import java.util.Arrays;

public class CExtendArchiveInfo {
    private int libraryCode;
    private String extendInfo;

    public CExtendArchiveInfo(int libcode) {
        this.libraryCode = libcode;
        this.extendInfo = "";
    }

    public CExtendArchiveInfo(int libcode, String extendinfo) {
        this.libraryCode = libcode;
        this.extendInfo = extendinfo;
    }

    public int getLibraryCode() {
        return libraryCode;
    }

    public void setLibraryCode(int libraryCode) {
        this.libraryCode = libraryCode;
    }

    public String getExtendInfo() {
        return extendInfo;
    }

    public void setExtendInfo(String extendInfo) {
        this.extendInfo = extendInfo;
    }

    public byte[] getByte() throws Exception {
        try {
            switch (libraryCode) {
                case 209:
                    return getByte_LiaoNingDAG();
            }
            return null;
        } catch (Exception ex) {
            throw new Exception("CExtendArchiveInfo getByte error:" + ex.getMessage());
        }
    }

    public boolean setInfo(byte[] bydata) throws Exception {
        try {
            switch (libraryCode) {
                case 209:
                    return setByte_LiaoNingDAG(bydata);
            }
            return false;
        } catch (Exception ex) {
            throw new Exception("CExtendArchiveInfo setByte error:" + ex.getMessage());
        }
    }

    private byte[] getByte_LiaoNingDAG() {
        if (extendInfo == null) {
            return null;
        }
        CBitClass bits_data = new CBitClass(18);
        String[] strInfo = extendInfo.split("\\|");
        if (strInfo.length < 8) {
            return null;
        }
        String szQZH = strInfo[0];
        if (szQZH.length() < 5) {
            szQZH = String.format("%-5s", szQZH).replace(' ', ' ');
        }
        szQZH = szQZH.substring(0, 5);
        bits_data.AddBits(szQZH);
        long tmpLong = 0;
        String tmpStr = strInfo[1];
        if (tmpStr.length() < 2) {
            tmpStr = String.format("%2s", tmpStr).replace(' ', '0');
        }
        tmpStr = tmpStr.substring(0, 2);
        tmpLong = Long.parseLong(tmpStr);
        bits_data.AddBits(tmpLong, 8);
        tmpStr = strInfo[2];
        String tmpJH = "";
        String tmpZH = "";
        String tmpCH = "";
        tmpStr = tmpStr.replace(")", "");
        int indexKH = tmpStr.indexOf('(');
        int indexCH = tmpStr.indexOf('-');
        if (indexKH >= 0) {
            tmpJH = tmpStr.substring(0, indexKH);
            if (indexCH >= 0) {
                tmpZH = tmpStr.substring(indexKH + 1, indexCH - indexKH - 1);
                tmpCH = tmpStr.substring(indexCH + 1, tmpStr.length() - indexCH - 1);
            } else {
                tmpZH = tmpStr.substring(indexKH + 1, tmpStr.length() - indexKH - 1);
            }
        } else {
            if (indexCH > 0) {
                tmpJH = tmpStr.substring(0, indexCH);
            } else {
                tmpJH = tmpStr;
            }
        }
        if (tmpJH.length() < 10) {
            tmpJH = String.format("%10s", tmpJH).replace(' ', '0');
        }
        tmpJH = tmpJH.substring(0, 10);
        if (tmpZH.length() < 2) {
            tmpZH = String.format("%2s", tmpZH).replace(' ', '0');
        }
        tmpZH = tmpZH.substring(0, 2);
        tmpLong = Long.parseLong(tmpJH);
        bits_data.AddBits(tmpLong, 34);
        tmpLong = Long.parseLong(tmpZH);
        bits_data.AddBits(tmpLong, 8);
        tmpStr = strInfo[3];
        if (tmpStr.length() < 3) {
            tmpStr = String.format("%3s", tmpStr).replace(' ', '0');
        }
        tmpLong = Long.parseLong(tmpStr);
        bits_data.AddBits(tmpLong, 10);
        tmpStr = strInfo[4];
        if (tmpStr.length() < 3) {
            tmpStr = String.format("%3s", tmpStr).replace(' ', '0');
        }
        tmpLong = Long.parseLong(tmpStr);
        bits_data.AddBits(tmpLong, 10);
        tmpStr = strInfo[5];
        if (tmpStr.length() < 2) {
            tmpStr = String.format("%2s", tmpStr).replace(' ', '0');
        }
        tmpLong = Long.parseLong(tmpStr);
        bits_data.AddBits(tmpLong, 8);
        tmpStr = strInfo[6];
        if (tmpStr.length() < 2) {
            tmpStr = String.format("%2s", tmpStr).replace(' ', '0');
        }
        tmpLong = Long.parseLong(tmpStr);
        bits_data.AddBits(tmpLong, 8);
        tmpStr = strInfo[7];
        if (tmpStr.length() < 4) {
            tmpStr = String.format("%4s", tmpStr).replace(' ', '0');
        }
        tmpLong = Long.parseLong(tmpStr);
        bits_data.AddBits(tmpLong, 14);
        byte[] byData = new byte[18];
        bits_data.GetBytes(byData);
        return byData;
    }

    private boolean setByte_LiaoNingDAG(byte[] byData) {
        if (byData.length < 18) {
            return false;
        }
        String data = "";
        int start_byte_position = 0;
        String tmpStr = "";
        for (int i = 0; i < 5; i++) {
            tmpStr += (char) byData[start_byte_position];
            start_byte_position++;
        }
        data += tmpStr.trim();
        long tmpLong = 0;
        byte[] tmpByte = Arrays.copyOfRange(byData, 5, 18);
        CBitClass bits_data = new CBitClass(tmpByte);
        tmpLong = bits_data.GetValue(0, 8);
        data += "|" + tmpLong;
        tmpLong = bits_data.GetValue(8, 34);
        data += "|" + tmpLong;
        tmpLong = bits_data.GetValue(42, 8);
        if (tmpLong > 0) {
            data += "(" + tmpLong + ")";
        }
        tmpLong = bits_data.GetValue(100, 4);
        if (tmpLong > 0) {
            data += "-" + tmpLong;
        }
        tmpLong = bits_data.GetValue(50, 10);
        data += "|" + tmpLong;
        tmpLong = bits_data.GetValue(60, 10);
        data += "|" + tmpLong;
        tmpLong = bits_data.GetValue(70, 8);
        data += "|" + tmpLong;
        tmpLong = bits_data.GetValue(78, 8);
        data += "|" + tmpLong;
        tmpLong = bits_data.GetValue(86, 14);
        data += "|" + tmpLong;
        extendInfo = data;
        return true;
    }
}


